<?php

/**
 * Вся работа с тендерами здесь
 *
 * @author kingop
 */
class TendersController extends CustomControllerAction {

    public function indexAction() {
        $tender = DatabaseObject_Tender::GetTenders($this->db);
        $cats = DatabaseObject_Categories::GetCategories($this->db);
        $project = DatabaseObject_Project::GetProjects($this->db);
        $this->view->tenders=$tender;
        $this->view->cats = $cats;
        $this->view->projects = $project;
    }

    public function viewtenderAction() {
        $id = $this->getRequest()->getParam('id');
        $tender = new DatabaseObject_Tender($this->db);
        $tender->load($id);
        
        $canSendRequest = DatabaseObject_Requests::CanSendRequest($this->db, Zend_Auth::getInstance()->getIdentity()->id, $tender->getId());
        
        $this->view->canSendRequest = $canSendRequest;
        
        $tenders = DatabaseObject_Tender::FilterIds($this->db, array('where' => 'idCat = ' . $tender->idCat));
        
        $usersRequests = DatabaseObject_Requests::GetRequests($this->db, array('where' => 'idUser = ' . Zend_Auth::getInstance()->getIdentity()->id . ' and idTender in(' . $tenders . ')'));
        
        if(count($usersRequests) > 0) {
            // пользовател ьуже загружал документы в этой категории
            // предоставим ему возможность выбора
            $this->view->makeChoise = true;
            $this->view->usersRequest = $usersRequests;
        }
        
        $fp = new FormProcessor_SendRequest($this->db);
        
        if ($this->getRequest()->isPost()) {
            if ($fp->process($this->getRequest())) {
                // отправим уведомление админам о новой заявке
                $emails = $this->configuration['admin_email'];
                $subject = $this->configuration['admin_email_request_subject'];
                
                $view_request_link = '<a href="http://' . $_SERVER['SERVER_NAME'] . '/admin/requests/' . $fp->request->getId() . '">Просмотеть заявку</a>';
                
                $text = preg_replace("/%viewlink%/", $view_request_link, $this->configuration['admin_email_request_text']);
                
                mail($emails, $subject, $text);
                
                $this->_redirect($this->getCustomUrl(array(), 'sendrequestcomplete'));
            }
            $this->view->showForm = true;
        }
        
        $this->view->fp = $fp;
        $this->view->tender = $tender;
        $this->view->tenders = DatabaseObject_Tender::FilterTenders($this->db, array('idProject'=>array($tender->idProject),'archive' => '0'), array('limit' => '4'));
        $this->view->page_title = $tender->title;
        $this->view->page_keywords = $tender->keywords;
        $this->view->page_description = $tender->description;
    }

    public function gettryingAction() {
        $tenderId = $this->getRequest()->getParam('id');
        
        
    }
      public function addfileAction() {

        ////Загрузка 
        if ($_FILES['upload']) {
            $upl = @$_FILES['upload'];
            $ufilename = $upl['name'];
            $path = DatabaseObject_RequestFiles::GetUploadDirectory();
            $filename = $ufilename;
            $file = $path . '/' . $filename;
            if (move_uploaded_file($_FILES['upload']['tmp_name'], $file)) {
                echo $filename;
            } else {
                echo "error";
            }
        }
        exit;
    }
    
    public function deletefileAction() {

        ////Загрузка 
        $filename = $this->getRequest()->getPost(id);
        $id = explode('-', $filename);
        echo $id['0'];
        $path = DatabaseObject_RequestFiles::GetUploadDirectory();
        $file = $path . '/' . $filename;
        DatabaseObject_RequestFiles::DeleteRequestFileID($this->db, $id['0']);
        if (file_exists($file)) {
            if (unlink($file)) {

                echo 'yes';
            } else {
                echo "error";
            }
        } else {
            echo 'error1';
        }
        exit;
    }

}

?>
